const router = require('koa-router')()

router.prefix('/coin')
// 获取用户硬币记录
router.post('/getcoin', async (ctx, next) => {
    let userid = ctx.request.body.userid;
    let sql = "select * from coin where userid=? order by date desc"
    let result = await ctx.db.EXCUTE(sql, [userid])
    ctx.body = {
        code: 200,
        data: result
    }
})


//backend
//页面初始化时获取数据库内的数据条目
router.post('/getcoinbackend', async (ctx, next) => {
    let result = await ctx.db.EXCUTE("SELECT n.*, (SELECT username FROM user ni WHERE n.userid=ni.userid) AS userinfo FROM coin n")
    
    ctx.body = {
        code: 200,
        data: result
    }
})


// 前端，用户拿到其用户数据
//backend
//页面初始化时获取数据库内的数据条目
router.post('/getmycoin', async (ctx, next) => {
    let data = ctx.request.body
    console.log(data);
    let result = await ctx.db.EXCUTE("select * from coin where userid=? order by date desc", [data.userid])
    console.log(result);

    ctx.body = {
        code: 200,
        data: result[0].sumcoin
    }
})


module.exports = router
